package com.ruoyi.lm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lm.domain.*;
import com.ruoyi.lm.service.*;
import org.apache.catalina.util.ServerInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务订单Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/phone/lm/serinfo")
public class LmSerinfoControllerPhone extends BaseController
{

    @Autowired
    private ILmSerinfoService lmSerinfoService;
    @Autowired
    private ILmUsersignService lmUsersignService;
    @Autowired
    private ILmUserService lmUserService;
    @Autowired
    private ILmTimesrecordService lmTimesrecordService;
    @Autowired
    private ILmRequireService lmRequireService;

    /**
     * 查询服务订单列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody LmSerinfo lmSerinfo)
    {
        List<LmSerinfo> list = lmSerinfoService.selectLmSerinfoList(lmSerinfo);
        return getDataTable(list);
    }

    /**
     * 统计首页数量
     */
    @PostMapping("/getCount")
    @ResponseBody
    public AjaxResult getCount()
    {
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("requireNum",lmRequireService.getRequireCount());
        resultMap.put("serviceNum",lmSerinfoService.getServiceCount());
        resultMap.put("clickNum",lmRequireService.getClickNum());
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",resultMap);
    }

    /**
     * 增加点击量
     */
    @PostMapping("/addClickNum")
    @ResponseBody
    public AjaxResult addClickNum(String id)
    {
        LmSerinfo lmSerinfo1 = lmSerinfoService.selectLmSerinfoById(id);
        LmSerinfo lmSerinfo = new LmSerinfo();
        lmSerinfo.setId(id);
        lmSerinfo.setClickNum(lmSerinfo1.getClickNum()+1);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",lmSerinfoService.updateLmSerinfo(lmSerinfo));
    }

    /**
     * 查询服务需求活动报名者信息
     */
    @PostMapping("/listSerinfoByUserId")
    @ResponseBody
    public AjaxResult listSerinfoByUserId(String serinfoId,String userId)
    {
        LmSerinfo lmSerinfo = lmSerinfoService.selectLmSerinfoById(serinfoId);
        LmUser lmUser = lmUserService.selectLmUserById(lmSerinfo.getUserId());
        //判断当前用户次数是否够用
        if(lmUser.getPackageTimes()>0){
            //减掉一次
            lmUser.setPackageTimes(lmUser.getPackageTimes()-1);
            lmUserService.updateLmUser(lmUser);
            //添加次数记录
            //0表示报名需求
            LmTimesrecord lmTimesrecord = new LmTimesrecord();
            lmTimesrecord.setId(IdWorker.getId());
            lmTimesrecord.setRecordType("1");//减少
            lmTimesrecord.setRecordUser(lmUser.getId());
            lmTimesrecord.setUserId(userId);
            lmTimesrecord.setServiceId(serinfoId);
            lmTimesrecord.setTimes(1);
            lmTimesrecordService.insertLmTimesrecord(lmTimesrecord);
            //返回报名用户信息
            return new AjaxResult(AjaxResult.Type.SUCCESS,"200",lmUserService.selectLmUserById(userId));
        }else{
            return error("次数已用完，请重新购买!");
        }
    }

    /**
     * 获取服务电话信息
     */
    @PostMapping("/getSerinfoPhone")
    @ResponseBody
    public AjaxResult getSerinfoPhone(String serinfoId,String userId,String buserId)
    {
        LmSerinfo lmSerinfo = lmSerinfoService.selectLmSerinfoById(serinfoId);
        LmUser lmUser = lmUserService.selectLmUserById(userId);
        //判断当前用户次数是否够用
        if(lmUser.getPackageTimes()>0){
            //减掉一次
            lmUser.setPackageTimes(lmUser.getPackageTimes()-1);
            lmUserService.updateLmUser(lmUser);
            //添加次数记录
            LmTimesrecord lmTimesrecord = new LmTimesrecord();
            lmTimesrecord.setId(IdWorker.getId());
            lmTimesrecord.setRecordType("1");//减少
            lmTimesrecord.setRecordUser(lmUser.getId());
            lmTimesrecord.setServiceId(serinfoId);
            lmTimesrecord.setTimes(1);
            lmTimesrecord.setUserId(buserId);
            lmTimesrecordService.insertLmTimesrecord(lmTimesrecord);

            LmSerinfo lmSerinfo1 = new LmSerinfo();
            lmSerinfo1.setId(lmSerinfo.getId());
            String clickPhone = String.valueOf(lmSerinfo.getClickPhoneNum());
            if(EmptyUtils.isNotEmpty(clickPhone)){
                lmSerinfo1.setClickPhoneNum(lmSerinfo.getClickPhoneNum() + 1);
            }else{
                lmSerinfo1.setClickPhoneNum(0);
            }
            lmSerinfoService.updateLmSerinfo(lmSerinfo1);

            //返回需求信息
            return new AjaxResult(AjaxResult.Type.SUCCESS,"200", lmSerinfo);
        }else{
            return error("次数已用完，请重新购买!");
        }
    }

  /*  *//**
     * 根据服务ID查询服务案例列表
     *//*
    @PostMapping("/listChild")
    @ResponseBody
    public TableDataInfo listChild(String id)
    {
        LmSerinfocase lmSerinfocase = new LmSerinfocase();
        lmSerinfocase.setServiceId(id);
        List<LmSerinfocase> childList = lmSerinfocaseService.selectLmSerinfocaseList(lmSerinfocase);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(childList);
        return  rspData;
    }*/

    /**
     * 根据服务ID查询服务详情+案例
     */
    @PostMapping("/listAllChild")
    @ResponseBody
    public TableDataInfo listAllChild(String id)
    {
        List<Map> resultList = new ArrayList<>();
        //添加服务详情
        Map temp = new HashMap();
        temp.put("detail",lmSerinfoService.selectLmSerinfoById(id));
        //获取服务报名人数
        LmUsersign lmUsersign = new LmUsersign();
        lmUsersign.setSignId(id);
        List<LmUsersign> childList = lmUsersignService.selectLmUsersignList(lmUsersign);
        temp.put("sign",childList.size());
        resultList.add(temp);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(resultList);
        return  rspData;
    }

    /**
     * 根据服务ID查询服务详情+
     */
    @PostMapping("/listDetail")
    @ResponseBody
    public TableDataInfo listDetail(String id)
    {
        List<Object> resultList = new ArrayList<>();
        //添加服务详情
        resultList.add(lmSerinfoService.selectLmSerinfoById(id));

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(resultList);
        return  rspData;
    }

    /**
     * 根据服务分类ID查询服务列表
     */
    @PostMapping("/listServiceByTypeId")
    @ResponseBody
    public TableDataInfo listServiceByTypeId(String typeId)
    {
        LmSerinfo lmSerinfo = new LmSerinfo();
        lmSerinfo.setServiceType(typeId);
        List<LmSerinfo> lmSerinfoList = lmSerinfoService.selectLmSerinfoList(lmSerinfo);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(lmSerinfoList);
        return  rspData;
    }


    /**
     * 新增保存服务订单
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody LmSerinfo lmSerinfo)
    {
        //判断当前用户是否已经实名认证
        LmUser lmUser = lmUserService.selectLmUserById(lmSerinfo.getUserId());
        if("0".equals(lmUser.getIsServiceuser())){
            //未认证不能发布
            return error("请先进行服务商认证!");
        }
        //获取图片
        String[] images = lmSerinfo.getServiceImages().split(",");

        lmSerinfo.setId(IdWorker.getId());
        lmSerinfo.setServicePic(images[0]);
        lmSerinfo.setClickNum(0);
        lmSerinfo.setServiceStatus("上架");
        int i = lmSerinfoService.insertLmSerinfo(lmSerinfo);
        if(i>0){
            return success("发布成功", lmSerinfo.getId());
        }
        return error();
    }

    /**
     * 修改保存服务订单
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody LmSerinfo lmSerinfo)
    {
        //获取图片
        String[] images = lmSerinfo.getServiceImages().split(",");
        lmSerinfo.setServicePic(images[0]);
        return toAjax(lmSerinfoService.updateLmSerinfo(lmSerinfo));
    }

    /**
     * 删除服务订单
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmSerinfoService.deleteLmSerinfoByIds(ids));
    }
}
